//
//  ViewController.m
//  ReactiveCocoa
//
//  Created by 李松 on 2017/11/28.
//  Copyright © 2017年 lisong. All rights reserved.
//

#import "ViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import "ViewModelClass.h"
#import "LSTableViewCell.h"
#import "Config.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong, nonatomic) UITableView *tableView;

@property(strong, nonatomic) NSArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view addSubview:self.tableView];
    [SVProgressHUD showWithStatus:@"加载ing"];
    ViewModelClass *viewModel = [[ViewModelClass alloc] init];
    [viewModel setBlockWithSucceedBlock:^(id succeedValue) {
        NSLog(@"%@", succeedValue);
        _dataArray = (NSArray *)succeedValue;
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } withErrorBlock:^(id errorValue) {
        NSLog(@"%@", errorValue);
        [SVProgressHUD dismiss];
    }];
    
   // [viewModel getWeiBoList];
    
    [viewModel ls_racGetList];
    
    [viewModel.requestCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];
    
    [[viewModel.requestCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if (![x boolValue]) {
            [SVProgressHUD dismiss];
        }
        
    }];
    
    [viewModel.requestCommand execute:nil];


}

- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        
        [_tableView registerNib:[UINib nibWithNibName:@"LSTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 115.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setValueWithDic:_dataArray[indexPath.row]];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
